package scau.edu.cn.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import scau.edu.cn.common.utils.ApiResponse;
import scau.edu.cn.entity.JudgeInfoEntity;
import scau.edu.cn.entity.JudgeInfoTypeEntity;
import scau.edu.cn.form.InfoType;
import scau.edu.cn.repository.JudgeInfoTypeRepository;
import scau.edu.cn.service.JudgeInfoTypeService;

import java.util.ArrayList;
import java.util.List;

/**
 * @description:
 * @author: yuxiang
 * @create: 2020-08-15 12:54
 **/
@Service
public class JudgeInfoTypeServiceImpl implements JudgeInfoTypeService {

    @Autowired
    private JudgeInfoTypeRepository judgeInfoTypeRepository;
    @Override
    public ApiResponse saveList(List<InfoType> infos) {
        ApiResponse apiResponse = new ApiResponse();
        try {
            List<JudgeInfoTypeEntity> entities = new ArrayList<>();

            if (infos.size() == 0){
                return new ApiResponse(-1,"信息列表为空");
            }
            for (InfoType info : infos) {
                entities.add(new JudgeInfoTypeEntity(info.getTypeName(),info.getName(), info.getScore(),info.getScoreType()));
            }
            //保存列表
            judgeInfoTypeRepository.saveAll(entities);

        } catch (Exception e) {
            e.printStackTrace();
            return new ApiResponse(-1, "服务器错误");
        }
        apiResponse.setMsg("保存成功");
        return apiResponse;
    }
}
